﻿package org.openChart.sprite.elements
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.openChart.display.DisplayElement;
	import flash.events.MouseEvent;
	import org.openChart.sprite.Color;
	import org.openChart.sprite.CommandHandler;
	
	/**
	 * The base class of bar chart element. Each bar chart element may need a horizontal line to
	 * mark the value.
	 * 
	 * @author Chongyuan
	 */
	public class BarElementSprite extends ElementSprite implements BaseBarElementInterface
	{
		//The horizontal line.
		protected var line:Sprite;
		
		
		
		/**
		 * The constructor of bar element sprite.
		 * 
		 * @param	displayObject The display model of bar chart element.
		 * @param	commandHandler The command handler of bar chart element.
		 */
		public function BarElementSprite( displayObject:DisplayElement, commandHandler:CommandHandler ) 
		{
			color = displayObject.color;
			super(displayObject, commandHandler);
			//this.alpha = 0.6;//displayObject.alpha;
			initLine();
		}
		
		/**
		 * The function is used to initialize the line sprite.
		 */
		public function initLine():void
		{
			//TODO
		}
		
		public override function mouseOver( event:MouseEvent ):void
		{
			super.mouseOver(event);
			/*if (displayElement.showValue == DisplayElement.SHOW_VALUE_SELETED)
			{
				text.visible = true;
			} 
			
			if (displayElement.showLine)
			{
				drawLine();
			}*/
		}
		
		public override function mouseOut( event:MouseEvent ):void
		{
			super.mouseOut(event);
			/*if (displayElement.showValue == DisplayElement.SHOW_VALUE_SELETED)
			{
				text.visible = false;
			} */
		}
		
		/**
		 * 
		 * @param	x_
		 * @param	y_
		 * @param	width_
		 * @param	height_
		 * @param	fill_color
		 * @param	fill_alpha
		 * @param	thickness
		 * @param	color
		 * @param	alpha
		 */
		protected function fillCube(x_:Number, y_:Number, width_:Number, height_:Number, fill_color:uint, fill_alpha:Number = 1, thickness:Number = 0, color:uint =0, alpha:Number = 1):void
		{
			var colorObject:Color = new Color(fill_color);
			
			fillRectangle(x_, y_, width_, height_, fill_color, fill_alpha, thickness, color, alpha);
			
			var position_1:Point = new Point(x_ + width_, y_);
			var position_2:Point = new Point(x_ + width_ + width_ / 2, y_ - width_ / 2);
			var position_3:Point = new Point(x_ + width_ + width_ / 2, y_ + height_ - width_ / 2);
			var position_4:Point = new Point(x_ + width_, y_ + height_);
			var positions_1:Array = [position_1, position_2, position_3, position_4];
			fillPolygon(positions_1, colorObject.getLighter(0.1), fill_alpha, thickness, color, alpha);
			
			var position_5:Point = new Point(x_, y_ + height_);
			var position_6:Point = new Point(x_ + width_ / 2, y_ + height_ - width_ / 2);
			var positions_2:Array = [position_3, position_4, position_5, position_6];
			fillPolygon(positions_2, colorObject.getLighter(0.3), fill_alpha, thickness, color, alpha);
		}
		
		/**
		 * 
		 * @param	x_
		 * @param	y_
		 * @param	width_
		 * @param	top_width
		 * @param	height_
		 * @param	fill_color
		 * @param	fill_alpha
		 * @param	thickness
		 * @param	color
		 * @param	alpha
		 */
		protected function fillCylindricalSurface(x_:Number, y_:Number, width_:Number, top_width:Number, height_:Number, fill_color:uint, fill_alpha:Number = 1, thickness:Number = 0, color:uint =0, alpha:Number = 1):void
		{
			graphics.beginFill(fill_color, fill_alpha);
			graphics.moveTo(x_, y_ - width_ / 4);
			drawEllipseArc(x_ + width_ / 2, y_ - width_ / 4, width_ / 2, width_ / 4, 180, 360, 0, 0);
			graphics.lineTo(x_ + width_ / 2 + top_width / 2, y_ - width_ / 4 - height_);
			drawEllipseArc(x_ + width_ / 2, y_ - width_ / 4 - height_, top_width / 2, top_width / 4, 180, 360, 1, 0);
			graphics.lineTo(x_, y_ - width_ / 4);
			graphics.endFill();
		}
		
		/**
		 * 
		 * @param	x_
		 * @param	y_
		 * @param	width_
		 * @param	height_
		 * @param	fill_color
		 * @param	fill_alpha
		 * @param	thickness
		 * @param	color
		 * @param	alpha
		 */
		protected function fillCylinder(x_:Number, y_:Number, width_:Number, height_:Number, fill_color:uint, fill_alpha:Number, thickness:Number = 0, color:uint =0, alpha:Number = 1):void
		{
			var colorObject:Color = new Color(fill_color);
			
			fillCylindricalSurface(x_, y_, width_, width_, height_, fill_color, fill_alpha, thickness, color, alpha);
			fillEllipse(x_ + width_ / 2, y_ - width_ / 4 - height_, width_ / 2, width_ / 4, colorObject.getLighter(0.2), fill_alpha, thickness, color, alpha);
		}
		
		/**
		 * 
		 * @param	x_
		 * @param	y_
		 * @param	width_
		 * @param	top_width
		 * @param	height_
		 * @param	fill_color
		 * @param	fill_alpha
		 * @param	thickness
		 * @param	color
		 * @param	alpha
		 */
		protected function fillCone(x_:Number, y_:Number, width_:Number, top_width:Number, height_:Number, fill_color:uint, fill_alpha:Number = 1, thickness:Number = 0, color:uint =0, alpha:Number = 1):void
		{
			var colorObject:Color = new Color(fill_color);
			fillCylindricalSurface(x_, y_, width_, top_width, height_, fill_color, fill_alpha, thickness, color, alpha);
			fillEllipse(x_ + width_ / 2, y_ - width_ / 4 - height_, top_width / 2, top_width / 4, colorObject.getLighter(0.2), fill_alpha, thickness, color, alpha);
		}
		
		/**
		 * 
		 * @param	x_
		 * @param	y_
		 * @param	width_
		 * @param	top_width
		 * @param	height_
		 * @param	fill_color
		 * @param	fill_alpha
		 * @param	thickness
		 * @param	color
		 * @param	alpha
		 */
		protected function fillPyramid(x_:Number, y_:Number, width_:Number, top_width:Number, height_:Number, fill_color:uint, fill_alpha:Number = 1, thickness:Number = 0, color:uint =0, alpha:Number = 1):void
		{
			var colorObject:Color = new Color(fill_color);
			
			var position_1:Point = new Point(x_, y_);
			var position_2:Point = new Point(x_ + width_, y_);
			var position_3:Point = new Point(x_ + width_ / 2 + top_width / 2, y_ - width_ / 4 - height_ + top_width / 4);
			var position_4:Point = new Point(x_ + width_ / 2 - top_width / 2, y_ - width_ / 4 - height_ + top_width / 4);
			var positions_1:Array = [position_1, position_2, position_3, position_4];
			fillPolygon(positions_1, fill_color);
			
			var position_5:Point = new Point(x_ + width_ + width_ / 2, y_ - width_ / 2);
			var position_6:Point = new Point(x_ + width_ / 2 + top_width, y_ - width_ / 4 - height_ - top_width / 4);
			var positions_2:Array = [position_2, position_5, position_6, position_3];
			fillPolygon(positions_2, colorObject.getLighter(0.1));
			
			var position_7:Point = new Point(x_ + width_ / 2, y_ - width_ / 4 - height_ - top_width / 4);
			var positions_3:Array = [position_4, position_3, position_6, position_7];
			fillPolygon(positions_3, colorObject.getLighter(0.3) );
		}
	}
}